<template>
  <div class="card">
    <div class="card-header" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filterFrom">
        <el-form-item>
          类型
          <el-select v-model="filterFrom.is_pay">
            <el-option label="全部" value=""></el-option>
            <el-option label="流出" value="0"></el-option>
            <el-option label="流入" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          状态
          <el-select v-model="filterFrom.status">
            <el-option label="全部" value=""></el-option>
            <el-option label="等待付款" value="0"></el-option>
            <el-option label="交易完成" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label='日期范围'>
          <el-date-picker type="daterange" range-separator="至" v-model="selectDate" placeholder="选择日期范围" :picker-options="datePickerOption"
            :editable="false"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="text" @click="showDialog = true"><i class="zmdi zmdi-filter-list" />&nbsp;更多筛选</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitFilter">确定</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card-body card-padding">
      <el-table :data="tableData" style="width: 100%" v-loading="isLoading" border>
        <el-table-column prop="id" label="ID" width="70"></el-table-column>
        <el-table-column prop="amount" label="金额" width="100"></el-table-column>
        <el-table-column :formatter="isPayFormatter" label="类型" width="100"></el-table-column>
        <el-table-column prop="order_number" label="对应订单编号" width="180"></el-table-column>
        <el-table-column :formatter="statusFormatter" label="状态" width="160"></el-table-column>
        <el-table-column prop="uid" width="130" label="相关用户ID"></el-table-column>
        <el-table-column prop="create_time" label="时间"></el-table-column>
      </el-table>
      <div class="table-pagination">
        <div style="float: left;margin-left: 20px;">
          <el-button type="text" @click="fetchData"><i class="zmdi zmdi-refresh" />&nbsp;&nbsp;重新加载</el-button>
        </div>
        <el-pagination :current-page="filterFrom.page_number" :page-sizes="[5, 10, 15, 20]" :page-size="filterFrom.page_size" layout="sizes, prev, pager, next, jumper,total"
          :total="totalSize" @size-change="handleSizeChange" @current-change="handlePageChange">
        </el-pagination>
      </div>
    </div>
    <el-dialog title="更多筛选" v-model="showDialog" size="tiny">
      <el-form ref="from" :model="filterFrom" label-width="80px">
        <el-form-item label="用户ID">
          <el-input v-model="filterFrom.uid"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.stop="resetForm">重 置</el-button>
        <el-button type="primary" @click.stop="fetchData">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import API from '../../api'
  import utils from '../../utils'

  var filterFrom = {
    start_time: '',
    end_time: '',
    status: '',
    is_pay: '',
    uid: '',
    page_number: 1,
    page_size: 10
  }

  export default {
    data: function () {
      return {
        showDialog: false,
        isLoading: false,
        tableData: [],
        totalSize: 0,
        filterFrom,
        datePickerOption: {
          disabledDate: function (time) {
            return time.getTime() > Date.now()
          }
        },
        selectDate: ''
      }
    },
    created: function () {
      this.fetchData()
    },
    methods: {
      handleSizeChange: function (val) {
        this.filterFrom.page_size = val
        this.fetchData()
      },
      handlePageChange: function (val) {
        this.filterFrom.page_number = val
        this.fetchData()
      },
      submitFilter: function () {
        this.showDialog = false
        if (this.selectDate) {
          this.filterFrom.start_time = utils.date.format(this.selectDate[0])
          this.filterFrom.end_time = utils.date.format(this.selectDate[1])
        } else {
          this.filterFrom.start_time = ''
          this.filterFrom.end_time = ''
        }
        this.fetchData()
      },
      fetchData: function () {
        this.isLoading = true
        this.showDialog = false
        API.billList(this.filterFrom,
          (data) => {
            this.isLoading = false
            if (!data) return
            this.tableData = data.list
            this.totalSize = data.total_size
          }
        )
      },
      resetForm: function () {
        this.filterFrom.uid = ''
      },
      isPayFormatter: function (row) {
        switch (row.is_pay) {
          case 0: return '流出'
          case 1: return '流入'
          default: return ''
        }
      },
      statusFormatter: function (row) {
        switch (row.status) {
          case 0: return '等待付款'
          case 1: return '交易完成'
          default: return ''
        }
      }
    }
  }

</script>
